//
//  HEGoodsDetailsCompanyCell.m
//  HealthElderly
//
//  Created by LOOK on 2020/10/24.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEGoodsDetailsCompanyCell.h"

@interface HEGoodsDetailsCompanyCell ()

@property(nonatomic, strong) UIImageView *logoImageView;

@property(nonatomic, strong) UILabel *nameLb;


@end

@implementation HEGoodsDetailsCompanyCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initUI];
    }
    return self;
}

- (void)initUI {
    _logoImageView = [UIImageView new];
    _logoImageView.contentMode = UIViewContentModeScaleAspectFill;
    _logoImageView.layer.masksToBounds = YES;
    _logoImageView.layer.cornerRadius = 8;
    [self.contentView addSubview:_logoImageView];
    
    _nameLb = [UILabel new];
    _nameLb.font = DYFont(18);
    _nameLb.numberOfLines = 0;
    _nameLb.text = @"--";
    [self.contentView addSubview:_nameLb];
    
    [_logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_offset(45);
        make.top.left.equalTo(self.contentView).mas_offset(15);
    }];
    
    [_nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_logoImageView);
        make.left.equalTo(_logoImageView.mas_right).mas_offset(10);
        make.right.equalTo(self.contentView).mas_offset(10);
        
    }];

    NSArray *checks = @[@"优质", @"平台认证", @"自营"];
    NSArray *checksColors = @[@"#35A954", @"#4690F8", @"#FF5555"];

    NSMutableArray *btnArr = [NSMutableArray array];

     //存放上一个button
     UIButton *buttonBefore;
     //存放button距离左侧的宽度，来确定是否需要换行
     __block CGFloat right = 0.0;
     CGFloat margin = 10.f; // 列间距
     CGFloat row_margin = 5.0f; // 行间距
     CGFloat max_width = KScreenWidth-(15+45+10); // 最大宽度
    
     for(int i = 0; i < checks.count; i++) {
         NSString *tag = checks[i];
         NSString *color = checksColors[i];
         
         UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
         backBtn.titleLabel.font = DYFont(12);
         [self.contentView addSubview:backBtn];
         [backBtn setTitle:tag forState:UIControlStateNormal];
         [backBtn setTitleColor:KHexColor(color) forState:UIControlStateNormal];
         [btnArr addObject:backBtn];
         
         backBtn.layer.borderWidth = 1;
         backBtn.layer.borderColor = KHexColor(color).CGColor;
         backBtn.layer.cornerRadius = 3;

         CGFloat fitWidth = [tag widthForFont:backBtn.titleLabel.font] + 10;

         [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
             make.width.mas_equalTo(fitWidth);
             make.height.mas_equalTo(20);
             if(i==0){
               make.left.equalTo(self.nameLb.mas_left);
               make.top.equalTo(self.nameLb.mas_bottom).offset(5);
               right = margin + fitWidth + margin;
             }else{
               if(right + fitWidth > max_width){ //换行
                   make.top.mas_equalTo(buttonBefore.mas_bottom).offset(row_margin);
                   make.left.equalTo(self.nameLb.mas_left).offset(margin);
                   right =  margin+fitWidth+margin;
               }else{
                   make.left.mas_equalTo(buttonBefore.mas_right).offset(margin);
                   make.top.equalTo(buttonBefore);
                   right += fitWidth+margin;
               }
             }

             if (i == (checks.count -1)) {
                 make.bottom.equalTo(self.contentView).mas_offset(-10);
             }
         }];
         buttonBefore = backBtn;
     }
}

- (void)refreshContent:(GDFiterCellModel *)fiterModel formModel:(GDFormModel *)formModel indexPath:(NSIndexPath *)indexPath {

    _nameLb.text = formModel.storeModel.name;
    [_logoImageView sd_setImageWithURL:[NSURL URLWithString:formModel.storeModel.logo] placeholderImage:IMAGE_NAMED(@"noImage")];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
